package org.example.controller;

import cn.hutool.core.util.ObjectUtil;
import com.ruoyi.common.core.web.domain.AjaxResult;
import org.example.domain.TMBank;
import org.example.service.TMBankService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("bank")
public class BankController {
    @Autowired
    TMBankService tmBankService;

    @Autowired
    RedisTemplate redisTemplate;
    /**
     * 银行类型
     * */
    @GetMapping("list")
    public AjaxResult list(){
        List<TMBank> list = (List<TMBank>) redisTemplate.opsForValue().get("bankList");
        if (ObjectUtil.isNull(list)){
            synchronized (this){
                if (!redisTemplate.hasKey("bankList")){
                    list = tmBankService.list();
                    redisTemplate.opsForValue().set("bankList",list,3*60, TimeUnit.MINUTES);
                }
            }
        }
        return AjaxResult.success(list);
    }
}
